package com.example.myapplication

import MainViewModel
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.myapplication.ui.theme.MyApplicationTheme
import coil.compose.rememberImagePainter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import androidx.compose.runtime.remember
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.unit.dp
import androidx.lifecycle.LiveData



class MainActivity : ComponentActivity() {

    private val viewModel: MainViewModel by viewModels()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            MyApplicationTheme {
                // 使用主题中的 'background' 颜色的 Surface 容器
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    ImageListContent()

                }
            }
        }
    }


    @Composable
    fun ImageListContent() {
        val imageUrls by viewModel.imageUrls.observeAsState()
        if (!imageUrls.isNullOrEmpty()) {
            ImageList(imageUrls!!)
        } else {
            Text("No images available") // Example placeholder text
        }
    }
    @Composable
    fun ImageList(imageUrls: List<String>) {
        LazyColumn {
            items(imageUrls) { imageUrl ->
                ImageWithCoil(imageUrl)
            }
        }
    }

    @Composable
    fun ImageWithCoil(imageUrl: String) {
        val painter = rememberImagePainter(data = imageUrl)

        Image(
            painter = painter,
            contentDescription = null,
            Modifier
                .fillMaxWidth()
                .height(300.dp)
        )
    }

    private fun updateUI() {
        runOnUiThread {
            // 当数据加载完成时更新 UI
            // 这是确保在主线程上进行 UI 更新的重要方法
            recreate()
        }
    }

    // 你的其余代码...
}


@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name",
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    MyApplicationTheme {
        Greeting("撒旦撒")
    }
}